package com.service.aop;

import com.alibaba.fastjson.JSONObject;
import com.common.annotation.RequestCheck;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Method;

@Component
@Aspect
public class ServiceAspect {

	// 定义一个全局的记录器，通过LoggerFactory获取
	private final static Logger logger = LoggerFactory.getLogger(ServiceAspect.class);

	@Around(value = "execution(* com.service.schedule..*.*(..))")
	public Object scheduleServiceAround(ProceedingJoinPoint point) throws Throwable {
		long begin = System.currentTimeMillis();

		Signature signature = point.getSignature();
		// 获取class注解信息
		RequestCheck requestCheckObj = point.getTarget().getClass().getAnnotation(RequestCheck.class);
		boolean isLog = requestCheckObj != null ? requestCheckObj.isLog() : false;
		// 获取method注解信息
		MethodSignature methodSignature = (MethodSignature) point.getSignature();
		Method method = methodSignature.getMethod();
		RequestCheck requestCheck = method.getAnnotation(RequestCheck.class);
		if (requestCheck != null) {
			isLog = requestCheck != null ? requestCheck.isLog() : false;
		}

		Object result = point.proceed(); // 调用目标方法
		logger.info("scheduleServiceAround调用类：{},调用方法:{},调用时间：{}", point.getSignature().getDeclaringTypeName(),
				point.getSignature().getName(), System.currentTimeMillis() - begin);
		return result;
	}

	@Around(value = "execution(* com.service..*.*(..))")
	public Object serviceAround(ProceedingJoinPoint point) throws Throwable {
		long begin = System.currentTimeMillis();

		Signature signature = point.getSignature();
		// 获取class注解信息
		RequestCheck requestCheckObj = point.getTarget().getClass().getAnnotation(RequestCheck.class);
		boolean isLog = requestCheckObj != null ? requestCheckObj.isLog() : false;
		// 获取method注解信息
		MethodSignature methodSignature = (MethodSignature) point.getSignature();
		Method method = methodSignature.getMethod();
		RequestCheck requestCheck = method.getAnnotation(RequestCheck.class);
		if (requestCheck != null) {
			isLog = requestCheck != null ? requestCheck.isLog() : false;
		}

		Object result = point.proceed(); // 调用目标方法
		logger.info("serviceAround调用类：{},调用方法:{},调用时间：{}", point.getSignature().getDeclaringTypeName(),
				point.getSignature().getName(), System.currentTimeMillis() - begin);
		return result;
	}

	@AfterThrowing(value = "execution(* com.service..*.*(..))", throwing = "e")
	public void doAfterThrowing(JoinPoint point, Throwable e) throws Throwable {
		System.out.println("service------------------->");
		long begin = System.currentTimeMillis();

		Signature signature = point.getSignature();
		// 获取class注解信息
		RequestCheck requestCheckObj = point.getTarget().getClass().getAnnotation(RequestCheck.class);
		// 获取method注解信息
		MethodSignature methodSignature = (MethodSignature) point.getSignature();
		Method method = methodSignature.getMethod();
		// 获取用户请求方法的参数并序列化为JSON格式字符串
		String params = "";
		if (point.getArgs() != null && point.getArgs().length > 0) {
			for (int i = 0; i < point.getArgs().length; i++) {
				Object object = point.getArgs()[i];
				if (!(object instanceof MultipartFile)) {
					params += JSONObject.toJSONString(point.getArgs()[i]) + ";";
				}
			}
		}
		logger.error("doAfterThrowing:{},调用类：{},调用方法:{},exception:{}", params,
				point.getSignature().getDeclaringTypeName(), point.getSignature().getName(), e);
	}

}
